﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
using System.IO;
using System.Collections;
namespace QuanLyThi
{
    public partial class FrmXemLaiBaiThi : Form
    {
        public static string mConnect = FrmMain.mConnect;
        private string msql = "";
        public DGC_Library mLibrary = new DGC_Library(mConnect);


        public FrmXemLaiBaiThi()
        {
            InitializeComponent();                    
        }
        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
       

        private void button6_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }


        private void txtMShocvien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                msql = "Select * from tb_hssv where ms_hssv='" + txtMShocvien.Text + "'";
                DataTable dt = mLibrary.GetDataTable(msql, "a");
                if (dt.Rows.Count > 0)
                {
                    txttenhocvien.Text = dt.Rows[0]["hovaten"].ToString();
                    msql = "Select distinct a.ms_mondaotao,b.mondaotao,a.ms_hssv from " +
                           " tb_hssvthi a inner join tb_mondaotao b on a.ms_mondaotao=b.ms_mondaotao " +
                           " Where a.ms_hssv='" + txtMShocvien.Text + "' order by b.mondaotao";
                    cmon.DataSource = mLibrary.GetDataTable(msql, "a");
                    cmon.DisplayMember = "mondaotao";
                    cmon.ValueMember = "ms_mondaotao";
                }
                else
                    MessageBox.Show("Mã học viên không tồn tại, vui lòng nhập cho chính xác", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        private void cmon_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                msql = "Select * from tb_loaihinhthi a " +
                 " Inner join tb_cauhinh_loaihinhthi b " +
                 " on a.ms_loaithi=b.ms_loaithi " +
                 " where b.ms_mondaotao=" + cmon.SelectedValue.ToString();
                chinhthuc.DataSource = mLibrary.GetDataTable(msql, "aa");
                chinhthuc.ValueMember = "ms_loaithi";
                chinhthuc.DisplayMember = "tenloaithi";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }


        private void chinhthuc_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                msql = "Select a.MS_HSSVthi, a.lanthi, case when a.ketqua=0 " +
                      "  then N'Hỏng' Else N'Đạt' End as Ketqua, a.Ngaythi, " +
                      "  rtrim(convert(char(4),a.Diemthi))+'/'+ rtrim(convert(char(4),b.Toida)) as Diemthi ," +
                      "   a.ketqua,b.Diemdat from tb_hssvthi a inner join tb_masodethi b on a.ms_mondaotao=b.ms_mondaotao " +
                      "   where a.ms_hssv='" + txtMShocvien.Text + "' and a.MS_Mondaotao=" + cmon.SelectedValue.ToString() + " and a.MS_loaithi=" + chinhthuc.SelectedValue.ToString() + " and " +
                      "   (a.Diemthi is not null or a.Ketqua is not null)";
                DataTable dt = mLibrary.GetDataTable(msql, "aa");
                if (dt.Rows.Count > 0)
                {
                    clanthi.DataSource = dt;
                    clanthi.ValueMember = "lanthi";
                    clanthi.DisplayMember = "lanthi";

                }
                else
                    clanthi.DataSource = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void clanthi_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                msql = "Select a.MS_HSSVthi, a.lanthi, case when a.ketqua=0 " +
                      "  then N'Hỏng' Else N'Đạt' End as Ketqua, a.Ngaythi, " +
                      "  rtrim(convert(char(4),a.Diemthi))+'/'+ rtrim(convert(char(4),b.Toida)) as Diemthi ," +
                      "   a.ketqua,b.Diemdat from tb_hssvthi a inner join tb_masodethi b on a.ms_mondaotao=b.ms_mondaotao " +
                      "   where a.ms_hssv='" + txtMShocvien.Text + "' and a.MS_Mondaotao=" + cmon.SelectedValue.ToString() + " and a.MS_loaithi=" + chinhthuc.SelectedValue.ToString() + " and " +
                      "  a.lanthi=" + clanthi.SelectedValue.ToString() + " and (a.Diemthi is not null or a.Ketqua is not null)";
                DataTable dt = mLibrary.GetDataTable(msql, "aa");
                if (dt.Rows.Count > 0)
                {
                    lblngaythi.Text = "Ngày thi " + DateTime.Parse(dt.Rows[0]["ngaythi"].ToString()).ToString("dd/MM/yyyy");
                    lbldiemdat.Text = "Điểm đạt " + dt.Rows[0]["diemdat"].ToString();
                    lbldiemthi.Text = "Điểm thi " + dt.Rows[0]["diemthi"].ToString();
                    lblketqua.Text = "Kết quả " + dt.Rows[0]["ketqua"].ToString();

                    string ms_hssv = dt.Rows[0]["MS_HSSVthi"].ToString();
                    this.ms_hssv = ms_hssv;
                    LoadListView(ms_hssv);
                }
                else
                {
                    lblngaythi.Text = "Ngày thi ";
                    lbldiemdat.Text = "Điểm đạt ";
                    lbldiemthi.Text = "Điểm thi ";
                    lblketqua.Text = "Kết quả ";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }


        string ms_hssv = "";
        private void LoadListView(string ms_hssv)
        {
            try
            {
                msql = " Select cauhoiID, N'Câu hỏi ' +rtrim(convert(char,cauhoiID)) as Noidung from tb_hssvthiCT " +
                       " where ms_hssvthi='" + ms_hssv + "' and lanthi=" + clanthi.SelectedValue.ToString() + " Order by cauhoiID";
                DataTable dtable = mLibrary.GetDataTable(msql, "a");
                if (dtable.Rows.Count > 0)
                {
                    list.AutoGenerateColumns = false;
                    list.DataSource = dtable;
                }

            }
            catch (Exception ex)
            {
                 MessageBox.Show(ex.Message.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void LoadNoidung(RichTextBox rt, byte[] noidung)
        {
            Stream theFile = new MemoryStream(noidung);
            rt.LoadFile(theFile, RichTextBoxStreamType.RichText);
        }



        private void list_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                msql = "Select a.dapan,a.traloi,c.* from tb_hssvthiCT a inner join tb_hssvthi b on a.MS_hssvthi=b.MS_hssvthi " +
                       " inner join tb_Nganhangdethi c on b.ms_mondaotao=c.ms_mondaotao and a.cauhoi=c.cauhoi where a.ms_hssvthi=" + ms_hssv +
                       " and a.cauhoiID=" + list.CurrentRow.Cells["Column1"].Value.ToString() + " and a.lanthi=" + clanthi.SelectedValue.ToString();
                DataTable dt = mLibrary.GetDataTable(msql, "a");
                //noi dung
                lblcau.Text = "Câu hỏi " + list.CurrentRow.Cells["Column1"].Value.ToString();

                panel4.Controls.Clear();

                RichTextBox ctrNoidung = new RichTextBox();
                panel4.Controls.Add(ctrNoidung);
                ctrNoidung.Location = new System.Drawing.Point(46, 1);
                ctrNoidung.Name = "txtNoidung";
                ctrNoidung.ReadOnly = true;
                ctrNoidung.Size = new System.Drawing.Size(808, 46);
                ctrNoidung.TabIndex = 0;
                ctrNoidung.Text = "";
                LoadNoidung(ctrNoidung, (byte[])dt.Rows[0]["noidung"]);
                GetTraLoi(dt.Rows[0]["traloi"].ToString().Trim());

                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    int hh = 45 * i;
                    RichTextBox ctrDapan = new RichTextBox();
                    panel4.Controls.Add(ctrDapan);
                    ctrDapan.Location = new System.Drawing.Point(46, 11 + hh);
                    ctrDapan.Name = "txtNoidung" + i;
                    ctrDapan.ReadOnly = true;
                    ctrDapan.Size = new System.Drawing.Size(808, 46);
                    ctrDapan.TabIndex = 0;
                    // ctrDapan.Text = "";
                    LoadNoidung(ctrDapan, (byte[])dt.Rows[i]["noidung"]);

                    //check
                    string dapan = dt.Rows[i]["ttdapan"].ToString();

                    if (dt.Rows[0]["ttcauhoi"].ToString() == "0")//1 dap an
                    {
                        RadioButton DARadio = new RadioButton();
                        panel4.Controls.Add(DARadio);
                        DARadio.AutoSize = true;
                        DARadio.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                        DARadio.ForeColor = System.Drawing.Color.Red;
                        DARadio.Location = new System.Drawing.Point(3, 22 + hh);
                        DARadio.Name = "DARadio" + i;
                        DARadio.Size = new System.Drawing.Size(43, 19);
                        DARadio.TabIndex = 2;
                        DARadio.TabStop = true;
                        DARadio.UseVisualStyleBackColor = true;
                        if (dapan == "True") DARadio.Text = "ĐA";
                        if (aList.Count != 0)
                        {
                            for (int ak = 0; ak < aList.Count; ak++)
                            {
                                if (i == int.Parse(aList[ak].ToString()))
                                {
                                    DARadio.Checked = true;
                                    break;
                                }
                            }
                        }
                    }
                    else//nhieu dap an
                    {
                        CheckBox DACheck = new CheckBox();
                        panel4.Controls.Add(DACheck);
                        DACheck.AutoSize = true;
                        DACheck.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                        DACheck.ForeColor = System.Drawing.Color.Red;
                        DACheck.Location = new System.Drawing.Point(3, 22 + hh);
                        DACheck.Name = "DACheck" + i;
                        DACheck.Size = new System.Drawing.Size(43, 19);
                        DACheck.TabIndex = 2;
                        DACheck.TabStop = true;
                        DACheck.UseVisualStyleBackColor = true;
                        if (dapan == "True") DACheck.Text = "ĐA";
                        if (aList.Count != 0)
                        {
                            for (int ak = 0; ak < aList.Count; ak++)
                            {
                                if (i == int.Parse(aList[ak].ToString()))
                                {
                                    DACheck.Checked = true;
                                    break;
                                }
                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        ArrayList aList = new ArrayList();
        private ArrayList GetTraLoi(string Traloi)
        {
            aList.Clear();
            if (Traloi != "")
            {
                //1;2;5;8;9;6;
                if (Traloi.Length == 12)//6DA
                {
                    aList.Add(Traloi.Substring(0, 1));
                    aList.Add(Traloi.Substring(2, 1));
                    aList.Add(Traloi.Substring(4, 1));
                    aList.Add(Traloi.Substring(6, 1));
                    aList.Add(Traloi.Substring(8, 1));
                    aList.Add(Traloi.Substring(10, 1));
                }
                else if (Traloi.Length == 10)//5DA
                {
                    aList.Add(Traloi.Substring(0, 1));
                    aList.Add(Traloi.Substring(2, 1));
                    aList.Add(Traloi.Substring(4, 1));
                    aList.Add(Traloi.Substring(6, 1));
                    aList.Add(Traloi.Substring(8, 1));           
                }
                else if (Traloi.Length == 8)//4DA
                {
                    aList.Add(Traloi.Substring(0, 1));
                    aList.Add(Traloi.Substring(2, 1));
                    aList.Add(Traloi.Substring(4, 1));
                    aList.Add(Traloi.Substring(6, 1));                
                }
                else if (Traloi.Length == 6)//3DA
                {
                    aList.Add(Traloi.Substring(0, 1));
                    aList.Add(Traloi.Substring(2, 1));
                    aList.Add(Traloi.Substring(4, 1));  
                }
                else if (Traloi.Length == 4)//2DA
                {
                    aList.Add(Traloi.Substring(0, 1));
                    aList.Add(Traloi.Substring(2, 1));                
                }
                else if (Traloi.Length == 2)//1DA
                {
                    aList.Add(Traloi.Substring(0, 1));                   
                }
            }
            return aList;
        }

      

         
     
    }
}
